# encoding=utf-8
import cPickle as pickle
import json
import re
import time

import httplib2
from datetime import datetime
from scrapy import Selector
from scrapy.http import Request
from scrapy.spiders import CrawlSpider

from jobspider.items import JobItem, CompanyInfoItem, JobDetailItem
from jobspider.spiders import Util


class Spider(CrawlSpider):
    name = "zhaopinSpider"

    def start_requests(self):

        page = 1

        keyword = "java"

        # 一天前
        day_range = "1"

        # http://sou.zhaopin.com/jobs/searchresult.ashx?jl=广东&sm=0&p=%s&sf=0&st=99999&isadv=1&pd=1&kw=java
        base_url = "http://sou.zhaopin.com/jobs/searchresult.ashx?jl=广东&sm=0&p=%s&sf=0&st=99999&isadv=1"

        if keyword:
            base_url += "&kw=" + keyword

        if keyword:
            base_url += "&pd=" + day_range


        search_url = base_url % (page)

        print "search_url", search_url
        yield Request(url=search_url, callback=self.parse_search, meta={"page": str(page), "base_url": base_url})

    def parse_search(self, response):

        content = response.body
        sel = Selector(text=content)

        # print content

        trs = sel.xpath("//table[@class='newlist']/tr[not(@class)]")


        for tr in trs[1:len(trs)]:

            # print tr.extract()
            # raw_input()
            job_url = tr.xpath("td[@class='zwmc']/div/a/@href").extract_first()
            job_name = tr.xpath("td[@class='zwmc']/div/a/text()").extract_first()

            company_detail_url = tr.xpath("td[@class='gsmc']/a/@href").extract_first()
            company_name = tr.xpath("td[@class='gsmc']/a/text()").extract_first()

            salary =tr.xpath("td[@class='zwyx']/text()").extract_first()
            job_detail_address =tr.xpath("td[@class='gzdd']/text()").extract_first()
            if job_detail_address:
                city_district = job_detail_address.split("-")
            city = city_district[0]
            district = None
            if len(city_district) > 1:
                district = city_district[1]

            time = tr.xpath("td[@class='gxsj']/span/text()").extract_first()
            publish_time = Util.time_transfer(time)

            job = JobItem()

            job["job_url"] = job_url
            job["job_name"] = job_name

            job["company_name"] = company_name
            job["company_detail_url"] = company_detail_url

            job["city"] = city

            job["district"] = district

            job["job_detail_address"] = job_detail_address

            job["salary"] = salary

            job["publish_time"] = publish_time

            job["query_time"] = datetime.now().strftime("%Y-%m-%d-%H-%M")
            job["query_type"] = "智联招聘"

            yield job
            # yield companyinfo

        next_url = sel.xpath("//li[@class='pagesDown-pos']/a/@href").extract_first()
        if next_url:
            print next_url
            yield Request(url=next_url, callback=self.parse_search)

    def parse_job_detail(self, response):
        pass